﻿Public Class frmThemNhaCungCap
    Private newNhaCungCap As New tbNhaCungCap
    Private luuTamNCC As New tbNhaCungCap
    Event ThemNhaCungCapThanhCong()

    Private Sub frmThemNhaCungCap_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.KeyPreview = True
        CtrlNhaCungCap1.NhaCungCap = newNhaCungCap
    End Sub

    Private Sub frmThemNhaCungCap_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.F2 Then
            btnThem_Click(Nothing, Nothing)
        ElseIf e.KeyCode = Keys.F3 Then
            Me.Close()
        End If
    End Sub

    Private Sub btnThem_Click(sender As Object, e As EventArgs) Handles btnThem.Click
        CtrlNhaCungCap1.KryptonLabel1.Focus()
        dt.Refresh(Data.Linq.RefreshMode.OverwriteCurrentValues, dt.tbNhaCungCaps)
        If CtrlNhaCungCap1.txtMaNCC.Text = "" Then
            ComponentFactory.Krypton.Toolkit.KryptonMessageBox.Show("Mã nhà cung cấp không được để trống!", _
                                                                    "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
            CtrlNhaCungCap1.txtMaNCC.Focus()
            Exit Sub
        End If

        If CtrlNhaCungCap1.txtTenNCC.Text = "" Then
            ComponentFactory.Krypton.Toolkit.KryptonMessageBox.Show("Tên nhà cung cấp không được để trống!", _
                                                                    "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
            CtrlNhaCungCap1.txtTenNCC.Focus()
            Exit Sub
        End If

        If dt.tbNhaCungCaps.Where(Function(s) s.MaNhaCungCap = CtrlNhaCungCap1.txtMaNCC.Text).Count > 0 Then
            ComponentFactory.Krypton.Toolkit.KryptonMessageBox.Show("Mã nhà cung cấp đã tồn tại trong hệ thống!", _
                                                                    "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
            CtrlNhaCungCap1.txtMaNCC.Focus()
            CtrlNhaCungCap1.txtMaNCC.Select(0, CtrlNhaCungCap1.txtMaNCC.Text.Length)
            Exit Sub
        End If

        dt.tbNhaCungCaps.InsertOnSubmit(newNhaCungCap)
        dt.SubmitChanges()
        RaiseEvent ThemNhaCungCapThanhCong()

        If chkCopyNhanVien.Checked Then
            luuTamNCC = newNhaCungCap
        End If
        newNhaCungCap = New tbNhaCungCap

        If chkCopyNhanVien.Checked Then
            newNhaCungCap.TenNhaCungCap = luuTamNCC.TenNhaCungCap
            newNhaCungCap.MaNhaCungCap = luuTamNCC.MaNhaCungCap
            newNhaCungCap.idCuaHang = luuTamNCC.idCuaHang
            newNhaCungCap.SoDienThoai = luuTamNCC.SoDienThoai
            newNhaCungCap.DiaChi = luuTamNCC.DiaChi
            newNhaCungCap.CongNoDauKy = luuTamNCC.CongNoDauKy
        End If
    End Sub

    Private Sub btnDong_Click(sender As Object, e As EventArgs) Handles btnDong.Click
        Me.Close()
    End Sub
End Class